package cn.topevery.garden.dal.logic;

import cn.topevery.garden.dal.dao.ExtentDao;
import cn.topevery.garden.entity.po.Extent;
import cn.topevery.garden.entity.query.ExtentQuery;
import cn.topevery.garden.framework.base.BaseLogic;
import hw.topevery.framework.GeometryHelper;
import hw.topevery.framework.db.base.BaseEntityDao;
import hw.topevery.framework.db.entity.DataPager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 描述 The type Extent logic.
 *
 * @author code -generate
 * @created 2021 -08-25 10:56:00
 * @date 2021 -08-06
 */
@Service
public class ExtentLogic extends BaseLogic<Extent> {
    @Autowired
    ExtentDao extentDao;

    /**
     * 描述 Gets dao.
     *
     * @return the dao
     * @author wenzhao.lin
     * @created 2021 -08-25 10:56:00
     */
    @Override
    public BaseEntityDao<Extent, Integer> getDao() {
        return extentDao;
    }


    /**
     * 获取列表
     *
     * @param query the query
     * @return the data pager
     * @author wenzhao.lin
     * @created 2021 -08-25 10:56:00 List page data pager.
     */
    public DataPager<Extent> listPage(ExtentQuery query) {
        DataPager<Extent> extentDataPager = extentDao.listPage(query);
        for(Extent extent : extentDataPager.records){
            if(extent.geomBd09!=null) {
                extent.mapRangesPoints= GeometryHelper.getGeometryPoint(extent.geomBd09);
                extent.geomBd09=null;
            }
        }
        return extentDataPager;
    }

    /**
     * 描述 新增/更新.
     *
     * @param userId the user id
     * @param entity the entity
     * @return the boolean
     * @author wenzhao.lin
     * @created 2021 -08-23 14:02:35 Save or update boolean.
     */
    public  boolean saveOrUpdate(String userId, Extent entity){
        extentDao.saveOrUpdate(userId, entity);
        return true;
    }

    /**
     * 描述 Gets by id.
     *
     * @param id the id
     * @return the by id
     * @author wenzhao.lin
     * @created 2021 -08-25 10:56:00
     */
    public Extent getById(Integer id) {
        Extent entity= extentDao.getEntity(id);
        return entity;
    }

    /**
     * 根据自增id批量删除
     *
     * @param ids
     */
    public void deleteBatchById(String userId,Integer[] ids) {
        extentDao.delete(userId, ids);
    }

    /**
     * 描述 通过公园Id和名称获取片区ID
     *
     * @param parkId the park id
     * @param name the name
     * @return the park id by code and name
     * @author wenzhao.lin
     * @created 2021 -08-19 14:44:45
     */
    public Integer getExtentIdByParkIdAndName(Integer parkId, String name){
        return extentDao.getExtentIdByParkIdAndName(parkId, name);
    }

    /**
     * 描述 检查是否已经存在.
     *
     * @param fieldName 字段名
     * @param value     字段值
     * @return the boolean （true已存在，false不存在）
     * @author wenzhao.lin
     * @created 2021 -08-19 09:23:47 Is exit boolean.
     */
    public  boolean isExit(String fieldName, String value){
        return extentDao.isExit(fieldName, value);
    }

    public List<Map<String,Object>> options(ExtentQuery query) {
        return extentDao.options(query);
    }


    public List<Extent> getExtentsByLngLat(double lng,double lat, Integer distance){
        List<Extent> extents = extentDao.getExtentsByLngLat(lng,lat, distance);
        for(Extent extent : extents){
            if(extent.geomBd09!=null) {
                extent.mapRangesPoints= GeometryHelper.getGeometryPoint(extent.geomBd09);
                extent.geomBd09=null;
            }
        }
        return extents;
    }
}
